package com.dkf.springcloud.controller;


import com.dkf.springcloud.bean.User;
import com.dkf.springcloud.service.IMessageProvider;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class SendMessageController {

    @Resource
    private IMessageProvider messageProvider;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMessage")
    public String sendMessage(){
        return messageProvider.send();
    }

    @GetMapping("/sendMg")
    public String send(){
        User user = new User();
        user.setName("name");
        user.setPassword("123");
        user.setAddress("aaaaa");

        rabbitTemplate.setConfirmCallback((data, ack, cause) -> {
            if (!ack) {
                System.out.println("消息发送失败!" + cause + data.toString());
            } else {
                System.out.println("消息发送成功,消息ID：" + (data != null ? data.getId() : null));
            }
        });
        rabbitTemplate.convertAndSend("exchange_2", "aaaaa.weather", user);
        return "success";
    }
}
